using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Builder
{
  public partial class EstimateLevelGains : Form
  {
    private Dictionary<string, GLBAttribute> m_BasePlayerAttr;
    private Decimal StartLevel;

    public EstimateLevelGains(Dictionary<string, GLBAttribute> BasePlayerAttr, Archetype Position, Decimal CurrentLevel, List<Archetype> Positions)
    {
      InitializeComponent();
      cmbxPosition.Items.Clear();
      foreach (Archetype at in Positions)
      {
        cmbxPosition.Items.Add(at);
      }
      cmbxPosition.DisplayMember = "DisplayName";
      cmbxPosition.ValueMember = "DisplayName";
      m_BasePlayerAttr = BasePlayerAttr;
      cmbxPosition.SelectedItem = Position;
      StartLevel = CurrentLevel;
    }


    private void EstimateLevelGains_Load(object sender, EventArgs e)
    {
      foreach (KeyValuePair<string, GLBAttribute> kvp in m_BasePlayerAttr)
      {
        DataGridViewRow dgvr = new DataGridViewRow();
        dgvr.CreateCells(dtgrdvwGains);
        dgvr.Cells[0].Value = kvp.Key;
        dgvr.Cells[1].Value = kvp.Value.CurrentAmount;
        dgvr.Cells[2].Value = kvp.Value.CurrentAmount;
        dtgrdvwGains.Rows.Add(dgvr);
      }
      spnedtLevel.Value = StartLevel;
      nmrcupdwnEnd.Value = StartLevel + 10;
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
      CalculateGains();
    }
    private void spnedtLevel_ValueChanged(object sender, EventArgs e)
    {
      CalculateGains();
    }

    private void CalculateGains()
    {
      Dictionary<string, GLBAttribute> CopyofAttr = new Dictionary<string, GLBAttribute>();

      foreach (KeyValuePair<string, GLBAttribute> kvp in m_BasePlayerAttr)
      {
        if (((Archetype)cmbxPosition.SelectedItem).Majors.Contains(kvp.Value.AttributeName))
        {
          CopyofAttr.Add(kvp.Key, new GLBAttribute(kvp.Value.AttributeName, kvp.Value.CurrentAmount, 0, GLBAttribute.AttributeType.atPrimary));
        }
        else if (((Archetype)cmbxPosition.SelectedItem).Minors.Contains(kvp.Value.AttributeName))
        {
          CopyofAttr.Add(kvp.Key, new GLBAttribute(kvp.Value.AttributeName, kvp.Value.CurrentAmount, 0, GLBAttribute.AttributeType.atSecondary));
        }
        else
        {
          CopyofAttr.Add(kvp.Key, new GLBAttribute(kvp.Value.AttributeName, kvp.Value.CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
        }
      }

      PlayerBuild pb = new PlayerBuild(1, (int)spnedtLevel.Value, 100, 1, 100, 1, 1, 1, 99, 2, 2, false, "Odd", 1, 1, "Name");
      //Add initial base day
      PlayerDay InitDay = new PlayerDay(0, 0, 1, 1, 0, 0, 0, 99, CopyofAttr);
      pb.PlayerDays.Add(Utility.GetSeasonDayString(1, 1), InitDay);

      //Now boost 
      int BoostCount = (int)nmrcupdwnEnd.Value - (int)spnedtLevel.Value;
      for (int i = 1; i <= BoostCount; i++)
      {
        pb.Boost();
      }
      foreach (KeyValuePair<string, GLBAttribute> kvp in pb.PlayerDays[Utility.GetSeasonDayString(1, 1)].RunningBuildTotals)
      {
        foreach (DataGridViewRow dgvr in dtgrdvwGains.Rows)
        {
          if (dgvr.Cells[0].Value.ToString() == kvp.Key)
          {
            dgvr.Cells[2].Value = Math.Round(kvp.Value.CurrentAmount, 2).ToString();
            break;
          }
        }
      }
    }

    private void cmbxPosition_SelectedIndexChanged(object sender, EventArgs e)
    {
      Dictionary<string, GLBAttribute> NewBPA = new Dictionary<string, GLBAttribute>();

      Archetype at = (Archetype)cmbxPosition.SelectedItem;

      foreach (String attr in at.Majors)
      {
        NewBPA.Add(attr, new GLBAttribute(attr, m_BasePlayerAttr[attr].CurrentAmount, 0, GLBAttribute.AttributeType.atPrimary));
      }
      foreach (String attr in at.Minors)
      {
        NewBPA.Add(attr, new GLBAttribute(attr, m_BasePlayerAttr[attr].CurrentAmount, 0, GLBAttribute.AttributeType.atSecondary));
      }


      if (!NewBPA.ContainsKey("strength"))
      {
        NewBPA.Add("strength", new GLBAttribute("strength", m_BasePlayerAttr["strength"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("blocking"))
      {
        NewBPA.Add("blocking", new GLBAttribute("blocking", m_BasePlayerAttr["blocking"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("speed"))
      {
        NewBPA.Add("speed", new GLBAttribute("speed", m_BasePlayerAttr["speed"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("vision"))
      {
        NewBPA.Add("vision", new GLBAttribute("vision", m_BasePlayerAttr["vision"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("stamina"))
      {
        NewBPA.Add("stamina", new GLBAttribute("stamina", m_BasePlayerAttr["stamina"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("jumping"))
      {
        NewBPA.Add("jumping", new GLBAttribute("jumping", m_BasePlayerAttr["jumping"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("agility"))
      {
        NewBPA.Add("agility", new GLBAttribute("agility", m_BasePlayerAttr["agility"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("confidence"))
      {
        NewBPA.Add("confidence", new GLBAttribute("confidence", m_BasePlayerAttr["confidence"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("catching"))
      {
        NewBPA.Add("catching", new GLBAttribute("catching", m_BasePlayerAttr["catching"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("carrying"))
      {
        NewBPA.Add("carrying", new GLBAttribute("carrying", m_BasePlayerAttr["carrying"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("tackling"))
      {
        NewBPA.Add("tackling", new GLBAttribute("tackling", m_BasePlayerAttr["tackling"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("kicking"))
      {
        NewBPA.Add("kicking", new GLBAttribute("kicking", m_BasePlayerAttr["kicking"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("throwing"))
      {
        NewBPA.Add("throwing", new GLBAttribute("throwing", m_BasePlayerAttr["throwing"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      if (!NewBPA.ContainsKey("punting"))
      {
        NewBPA.Add("punting", new GLBAttribute("punting", m_BasePlayerAttr["punting"].CurrentAmount, 0, GLBAttribute.AttributeType.atNone));
      }
      m_BasePlayerAttr = NewBPA;
      CalculateGains();
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {

    }

    private void copyGridToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
    {
      dtgrdvwGains.SelectAll();
    }





  }
}